package au.com.handsunited.web.controller.entity;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.relationship.Relationship;
import au.com.handsunited.service.EntityService;

/**
 * @author Arun Chandrasekhar
 */
public class OrganisationNetworkViewController extends AbstractController {

	private EntityService entityService;
	private String successView;

	private static Logger logger = LoggerFactory.getLogger(OrganisationNetworkViewController.class);



	/**
	 *
	 */
	public OrganisationNetworkViewController() {
		// TODO Auto-generated constructor stub
	}



	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView();

		String entityId = request.getParameter("id");

		if (StringUtils.isNotBlank(entityId)) {

			BaseEntity entity = entityService.getEntity(entityId);

			if (entity != null) {
				Set<Relationship> organisationRelationships = entity.getOrganisationRelationships();
				mav.addObject("organisations", organisationRelationships);
				mav.setViewName(successView);
			}
		}

		return mav;
	}



	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
}
